<template>
  <div class="app-container">
    <el-form v-show="showSearch" ref="queryForm" :inline="true" :model="queryParams" label-width="68px" size="small">
      <el-form-item label="药品编码" prop="DrugsCode">
        <el-input
          v-model="queryParams.DrugsCode"
          clearable
          placeholder="请输入药品编码"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="药品名称" prop="DrugsName">
        <el-input
          v-model="queryParams.DrugsName"
          clearable
          placeholder="请输入药品名称"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="助记码" prop="MnemonicCode">
        <el-input
          v-model="queryParams.MnemonicCode"
          clearable
          placeholder="请输入拼音助记码"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button icon="el-icon-search" size="mini" type="primary" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          icon="el-icon-plus"
          plain
          size="mini"
          type="primary"
          @click="handleAdd"
        >添加
        </el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="DrugsList" @selection-change="handleSelectionChange">
      <el-table-column align="center" type="selection" width="55"/>
      <el-table-column align="center" label="id" prop="Drugs.Id"/>
      <el-table-column align="center" label="药品编码" prop="Drugs.DrugsCode"/>
      <el-table-column align="center" label="药品名称" prop="Drugs.DrugsName"/>
      <el-table-column align="center" label="药品规格" prop="Drugs.DrugsFormat"/>
      <el-table-column align="center" label="包装单位" prop="Drugs.DrugsUnit"/>
      <el-table-column align="center" label="生产厂家" prop="Drugs.Manufacturer"/>
      <el-table-column align="center" label="药品剂型" prop="DrugsDosage"/>
      <el-table-column align="center" label="药品类型" prop="DrugsType"/>
      <el-table-column align="center" label="药品单价" prop="Drugs.DrugsPrice"/>
    </el-table>

    <pagination
      v-show="total>0"
      :limit.sync="queryParams.size"
      :page.sync="queryParams.current"
      :total="total"
      @pagination="getList"
    />

  </div>
</template>

<script>
import {listDrugs} from "@/api/system/Drugs";
import {addPrescriptionDetailed} from "@/api/system/PrescriptionDetailed";
import {addPatientCosts} from "@/api/system/PatientCosts";

export default {
  name: "Drugs",
  data() {
    return {
      loading: true,
      rows: [],
      single: true,
      multiple: true,
      showSearch: true,
      total: 0,
      DrugsList: [],
      queryParams: {
        current: 1,
        size: 10,
        DrugsCode: null,
        DrugsName: null,
        MnemonicCode: null,
      },
    };
  },
  created() {
    this.getList();
  },
  methods: {
    getList() {
      this.loading = true;
      listDrugs(this.queryParams).then(response => {
        this.DrugsList = response.records;
        this.total = response.total;
        this.loading = false;
      });
    },
    handleQuery() {
      this.queryParams.current = 1;
      this.getList();
    },
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    handleSelectionChange(selection) {
      this.rows = selection
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    handleAdd() {
      this.$confirm("确定添加上述药物至处方吗？").then(() => {
        this.rows.forEach((row) => {
          let prescriptionDetailed = {
            Id: null,
            PrescriptionId: this.$route.query.PrescriptionId,
            DrugsId: row.Drugs.Id,
            Rules: null,
            Dosage: null,
            Frequency: null,
            Price: row.Drugs.DrugsPrice
          };
          addPrescriptionDetailed(prescriptionDetailed).then(() => {
            addPatientCosts({
              Id: null,
              RegisterId: this.$route.query.RegisterId,
              ItemId: 3,
              Price: prescriptionDetailed.Price,
              State: 0,
              DelMark: null
            })
          })
        })
        this.$alert("添加成功！").then(() => {
          this.$router.back()
        })
      })
    },
  }
};
</script>
